package com.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{
    private List<String> allowedpass;
	//在dispatcherServlet处理之后执行--清理工作
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	//在请求处理的方法执行之后执行
	@Override
	public void postHandle(HttpServletRequest req, HttpServletResponse rep, Object obj, ModelAndView mnv)
			throws Exception {
		// TODO Auto-generated method stub
		
	}

	//允许哪些url不被拦截，哪些需要被拦截
	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse rep, Object obj) throws Exception {
		// TODO Auto-generated method stub
		String url=req.getRequestURI().toString();
		//先判断session中是否有
		Object user=req.getSession().getAttribute("user");
		if(user!=null) return true;
		for(String temp:allowedpass){
			if(url.endsWith(temp)){
				return true;
			}
		}
		rep.sendRedirect(req.getContextPath() + "/login.jsp");
		return false;
	}

	public void setAllowedpass(List<String> allowedpass) {
		this.allowedpass = allowedpass;
	}
}
